package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import com.alibaba.fastjson.JSON;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.CommonFragmentPagerAdapter;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.ui.fragment.RankFragment;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.view.tablayout.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * 精选页面排行榜
 */

public class ChoiceRankActivity extends DiaobaoBaseActivity {
    private TabLayout tlTitle;
    private TTHomeViewPager mViewPager;
    List<Fragment> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_rank_layout);
        tlTitle = (TabLayout) findViewById(R.id.tlTitle);
        mViewPager = (TTHomeViewPager) findViewById(R.id.mViewPager);
        int normalColor = getResources().getColor(R.color.text_color_2);
        int selectColor = getResources().getColor(R.color.text_color_57);
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            normalColor = getResources().getColor(R.color.text_dark);
            selectColor = getResources().getColor(R.color.theme_main_dark);
        }
        tlTitle.setTabTextColors(normalColor,selectColor);
        tlTitle.setSelectedTabIndicatorColor(selectColor);
        tlTitle.setTabMode(TabLayout.MODE_SCROLLABLE);
        String data = getIntent().getStringExtra(Constant.DATA);
        int pageIndex = getIntent().getIntExtra(Constant.PAGE_INDEX, 0);
        List<DFHomeCategoryBean.Item> items = JSON.parseArray(data, DFHomeCategoryBean.Item.class);
        if (items != null && items.size() > 0) {
            String[] titles = new String[items.size()];
            for (int i = 0; i < items.size(); i++) {
                DFHomeCategoryBean.Item item = items.get(i);
                if (item == null) {
                    continue;
                }
                titles[i] = item.getTitle();
                boolean loadDataNow;
                if (i == pageIndex) {
                    loadDataNow = true;
                }else{
                    loadDataNow = false;
                }
                list.add(RankFragment.newInstance(item.getUrl(), loadDataNow));
            }

            CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), list, titles);
            mViewPager.setAdapter(adapter);
            mViewPager.setCurrentItem(pageIndex);
            tlTitle.setupWithViewPager(mViewPager);
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("position", String.valueOf(position));
                    TMAnalysis.event(ChoiceRankActivity.this, UmengEventName.JINGXUAN_RANK_TAB,map);
                    Fragment fragment = list.get(position);
                    if (fragment instanceof RankFragment) {
                        ((RankFragment) fragment).initGroupData();
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }

    }

}
